package com.ceac.kronos.ejb.entitybeans;

import com.ceac.kronos.ejb.entitybeans.Edition;

import java.io.Serializable;

import java.sql.Timestamp;

import java.util.Date;
import java.util.List;

import java.util.Vector;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.OneToMany;
import javax.persistence.OrderBy;
import javax.persistence.Table;
import javax.persistence.TableGenerator;

@Entity
@NamedQueries( { @NamedQuery(name = "Competition.findAll", query = "select o from Competition o"),
        @NamedQuery(name = "Competition.findByName", query = "select o from Competition o where o.name=:name"),
        @NamedQuery(name = "Competition.findById", query = "select o from Competition o where o.competitionId=:competitionId"),
        @NamedQuery(name = "Competition.findByCode", query = "select o from Competition o where o.code=:code") })
@Table(name = "COMPETITIONS")
public class Competition implements Serializable {
    private String city;
    @Column(nullable = false, unique=true, updatable=false, name = "CODE")
    private String code;
    @Id
    @Column(name="COMPETITION_ID", nullable = false)
    @GeneratedValue(strategy = GenerationType.TABLE, generator = "CompetitionGenerator")
    @TableGenerator(name="CompetitionGenerator", table="SEQUENCES",
        schema = "kronos", pkColumnName = "SEQUENCE_NAME",
                    valueColumnName = "SEQUENCE_COUNT", pkColumnValue="DEFAULT", initialValue=1,
                    allocationSize = 1)
    private Integer competitionId;
    private String county;
    @Column(name="CREATED_BY")
    private Integer createdBy;
    @Column(name="CREATION_DATE")
    private Timestamp creationDate;
    private String description;
    @Column(name="FIRST_EDITION_DATE")
    private Timestamp firstEditionDate;
    @Column(name="LAST_UPDATED_BY")
    private Integer lastUpdatedBy;
    @Column(name="LAST_UPDATE_DATE")
    private Timestamp lastUpdateDate;
    @Column(nullable = false)
    private String name;
    @Column(name="ORGANIZED_BY")
    private String organizedBy;
    @Column(name="POSTAL_CODE")
    private String postalCode;
    private String province;
    
    @OrderBy(value = "editionNumber")
    @OneToMany(targetEntity = Edition.class, mappedBy = "competition", fetch = FetchType.EAGER, cascade = CascadeType.ALL)
    @JoinColumn(columnDefinition = "COMPETITION_ID", referencedColumnName = "COMPETITION_ID", name = "COMPETITION_ID")
    private List<Edition> editions;
    
    public Competition() {
        this.editions = new Vector<Edition>();
        this.setCreationDate(new Timestamp((new Date()).getTime()));
        this.setLastUpdateDate(new Timestamp((new Date()).getTime()));
        this.setCreatedBy(0);
        this.setLastUpdatedBy(0);
    }

    public String getCity() {
        return city;
    }

    public void setCity(String city) {
        this.city = city;
    }

    public Integer getCompetitionId() {
        return competitionId;
    }

    public void setCompetitionId(Integer competitionId) {
        this.competitionId = competitionId;
    }

    public String getCounty() {
        return county;
    }

    public void setCounty(String county) {
        this.county = county;
    }

    public Integer getCreatedBy() {
        return createdBy;
    }

    public void setCreatedBy(Integer createdBy) {
        this.createdBy = createdBy;
    }

    public Timestamp getCreationDate() {
        return creationDate;
    }

    public void setCreationDate(Timestamp creationDate) {
        this.creationDate = creationDate;
    }

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    public Timestamp getFirstEditionDate() {
        return firstEditionDate;
    }

    public void setFirstEditionDate(Timestamp firstEditionDate) {
        this.firstEditionDate = firstEditionDate;
    }

    public Integer getLastUpdatedBy() {
        return lastUpdatedBy;
    }

    public void setLastUpdatedBy(Integer lastUpdatedBy) {
        this.lastUpdatedBy = lastUpdatedBy;
    }

    public Timestamp getLastUpdateDate() {
        return lastUpdateDate;
    }

    public void setLastUpdateDate(Timestamp lastUpdateDate) {
        this.lastUpdateDate = lastUpdateDate;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getOrganizedBy() {
        return organizedBy;
    }

    public void setOrganizedBy(String organizedBy) {
        this.organizedBy = organizedBy;
    }

    public String getPostalCode() {
        return postalCode;
    }

    public void setPostalCode(String postalCode) {
        this.postalCode = postalCode;
    }

    public String getProvince() {
        return province;
    }

    public void setProvince(String province) {
        this.province = province;
    }

    public void setEditions(List<Edition> editions) {
        this.editions = editions;
    }

    public List<Edition> getEditions() {
        return editions;
    }

    public void setCode(String code) {
        this.code = code;
    }

    public String getCode() {
        return code;
    }
    
    public void addEdition(Edition e) {
        this.getEditions().add(e);
        
        e.setCompetition(this);
    }
}
